package com.yanqu.road.web.controller.game;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.web.HttpJsonWebParam;
import com.yanqu.road.web.WebServer;
import com.yanqu.road.web.controller.BaseJsonHttpServlet;
import com.yanqu.road.webservice.invoke.GameWebServiceInvoke;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/queryServerOnline")
public class QueryServerOnline extends BaseJsonHttpServlet {

	private static Logger logger = LogManager.getLogger(QueryServerOnline.class.getName());

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info("request queryServerOnline");
		HttpJsonWebParam httpJsonWebParam = super.parseRequest(request);
		JSONObject paramObj = httpJsonWebParam.getParamObj();
		long count = GameWebServiceInvoke.queryServerOnline();
		JSONObject dataObj = new JSONObject();
		dataObj.put("count", count);

		JSONObject resultObj = new JSONObject();
		resultObj.put("" + WebServer.getInstance().getServerId(), dataObj);
		sendMessage(response, resultObj.toJSONString());
	}
}
